﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreValidationCode;
using TarreOA.TarreOAModels;
using TarreOA.TarreOABLL;

public partial class admin_NetDiskSetting_NetDiskCatalogEdit : System.Web.UI.Page
{
    private string pcps;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["nd_id"] != null)
            {
                int ndid = int.Parse(Request.QueryString.Get("nd_id"));
                this.BtnCreate.Attributes.Add("nd_id", ndid.ToString());
                NetDisktbServices ndService = new NetDisktbServices();
                NetDisktb netDisk = ndService.findByNdId(ndid);
                if (netDisk != null)
                {
                    this.txtCatalogName.Text = netDisk.NetDiskCatalogName;
                    pcps = netDisk.Pcps;
                    this.AddTreeViewData();
                }
            }
        }
    }

    /// <summary>
    /// 查找选定的部门节点
    /// </summary>
    /// <param name="node"></param>
    protected void FindCheckNode(TreeNode node)
    {
        string[] pcp = pcps.Split(',');
        for (int i = 0; i < pcp.Length; i++)
        {
            if (pcp[i] != null && pcp[i].Equals(node.Value))
            {
                node.Checked = true;
            }
        }
        if (node.ChildNodes.Count > 0)
        {
            foreach (TreeNode nd in node.ChildNodes)
            {
                FindCheckNode(nd);
            }
        }
    }

    protected void AddTreeViewData()
    {
        TreeNode root = new TreeNode();
        root.Text = "全体人员";
        root.Value = "0";
        root.SelectAction = TreeNodeSelectAction.Expand;
        root.ImageUrl = "../../images/Section.png";
        SectionServices sectionService = new SectionServices();
        List<Section> firSections = sectionService.findByBaseSectionId(0);
        if (firSections != null && firSections.Count > 0)
        {
            foreach (Section section in firSections)
            {
                TreeNode node = new TreeNode();
                node.Text = section.SectionName;
                node.Value = section.SectionId.ToString();
                node.ImageUrl = "../../images/Sections.png";
                AddTreeViewDataRecrusion(node, section.SectionId);
                if (node.ChildNodes.Count > 0)
                {
                    node.SelectAction = TreeNodeSelectAction.Expand;
                }
                else
                {
                    node.NavigateUrl = "#";
                }
                root.ChildNodes.Add(node);
            }
        }
        this.FindCheckNode(root);
        this.TreeView1.Nodes.Add(root);
    }

    /// <summary>
    /// 递归查找部门信息填充树节点
    /// </summary>
    /// <param name="node"></param>
    /// <param name="bsid"></param>
    protected void AddTreeViewDataRecrusion(TreeNode node, int bsid)
    {
        SectionServices sectionServices = new SectionServices();
        List<Section> baseSections = sectionServices.findByBaseSectionId(bsid);
        if (baseSections != null && baseSections.Count > 0)
        {
            foreach (Section section in baseSections)
            {
                TreeNode snode = new TreeNode();
                snode.Text = section.SectionName;
                snode.Value = section.SectionId.ToString();
                snode.ImageUrl = "../../images/Sections.png";
                AddTreeViewDataRecrusion(snode, section.SectionId);
                if (snode.ChildNodes.Count > 0)
                {
                    snode.SelectAction = TreeNodeSelectAction.Expand;
                }
                else
                {
                    snode.NavigateUrl = "#";
                }
                node.ChildNodes.Add(snode);
            }
        }
    }

    private string sids = "";
    protected void BtnCreate_Click(object sender, EventArgs e)
    {
        if (!Code.Codes.Equals(Request.Form.Get(this.TextBox1.ClientID)))
        {
            ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('验证码输入错误！')", true);
        }
        else
        {
            int ndid = int.Parse(((Button)sender).Attributes["nd_id"]);
            foreach (TreeNode node in this.TreeView1.Nodes)
            {
                if (node.Checked)
                {
                    sids += (node.Value + ",");
                }
                if (node.ChildNodes.Count > 0)
                {
                    this.findCheckedNode(node);
                }
            }
            string catalogName = Request.Form.Get(this.txtCatalogName.ClientID);
            NetDisktbServices netDiskService = new NetDisktbServices();
            if (netDiskService.UpdateCatalogInfo(catalogName, sids, ndid) == 1)
            {
                Response.Redirect("NetDiskIndex.aspx");
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('目录更新失败，请重试！');", true);
            }
        }
    }

    /// <summary>
    /// 递归查找选中的树节点
    /// </summary>
    /// <param name="node"></param>
    /// <param name="list"></param>
    protected void findCheckedNode(TreeNode node)
    {
        foreach (TreeNode snode in node.ChildNodes)
        {
            if (snode.Checked)
            {
                sids += (snode.Value + ",");
            }
            if (snode.ChildNodes.Count > 0)
            {
                findCheckedNode(snode);
            }
        }
    } 
}
